<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chentianming11.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":{"enable":true,"onlypost":false,"loadingImg":"/uploads/loading.gif"},"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Redis集群通过分片来进行数据共享，并提供复制和故障转移功能。本文将对集群的节点、槽指派、命令执行、重新分片、转向、故障转移等各个方面进行介绍。">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis分布式-集群详解">
<meta property="og:url" content="https://chentianming11.github.io/posts/2663121991/index.html">
<meta property="og:site_name" content="伍六七">
<meta property="og:description" content="Redis集群通过分片来进行数据共享，并提供复制和故障转移功能。本文将对集群的节点、槽指派、命令执行、重新分片、转向、故障转移等各个方面进行介绍。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-node.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-state.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-command.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-migrate.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-sharding.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-ask.png">
<meta property="og:image" content="https://chentianming11.github.io/images/redis/cluster-failover.png">
<meta property="article:published_time" content="2020-11-08T06:29:05.000Z">
<meta property="article:modified_time" content="2020-11-22T12:25:37.538Z">
<meta property="article:author" content="陈添明">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chentianming11.github.io/images/redis/cluster-node.png">

<link rel="canonical" href="https://chentianming11.github.io/posts/2663121991/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Redis分布式-集群详解 | 伍六七</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">伍六七</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">一起学Java</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">6</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">3</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">17</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/chentianming11" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chentianming11.github.io/posts/2663121991/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="陈添明">
      <meta itemprop="description" content="一个爱看国漫的程序猿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="伍六七">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis分布式-集群详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-11-08 14:29:05" itemprop="dateCreated datePublished" datetime="2020-11-08T14:29:05+08:00">2020-11-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-22 20:25:37" itemprop="dateModified" datetime="2020-11-22T20:25:37+08:00">2020-11-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/posts/2663121991/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/posts/2663121991/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>15 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><strong>Redis集群通过分片来进行数据共享，并提供复制和故障转移功能</strong>。本文将对集群的节点、槽指派、命令执行、重新分片、转向、故障转移等各个方面进行介绍。</p>
<a id="more"></a>

<blockquote>
<p>本文主要内容参考自《Redis设计与实现》</p>
</blockquote>
<h2 id="节点"><a href="#节点" class="headerlink" title="节点"></a>节点</h2><p><strong>一个Redis集群通常由多个节点(node)组成，在刚开始的时候，每个节点都是相互独立的，它们都处于一个只包含自己的集群当中</strong>。可以通过<code>CLUSTER MEET</code>命令来连接各个节点，从而构建一个包含多节点集群。命令格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CLUSTER MEET &lt;ip&gt; &lt;port&gt;</span><br></pre></td></tr></table></figure>

<p><strong>当一个节点执行<code>CLUSTER MEET</code>命令，就可以将对应<code>ip</code>和<code>port</code>的节点加入到自己当前所在的集群中</strong>（首先会进行握手，握手成功后才添加进来）。假设现在有三个节点，ip都是<code>127.0.0.1</code>，端口分别是<code>7000</code>、<code>7001</code>、<code>7002</code>。当在<code>7000</code>端口上的节点执行<code>CLUSTER MEET 127.0.0.1 7001</code>，就会将<code>7001</code>节点加入到<code>7000</code>节点所在的集群中。在<code>7000</code>端口上的节点再执行<code>CLUSTER MEET 127.0.0.1 7002</code>时，就会将<code>7002</code>节点也加入到<code>7000</code>节点所在的集群中。至此，这三个节点就组成了一个集群。<br><img data-src="https://chentianming11.github.io/images/redis/cluster-node.png" alt="cluster-node"></p>
<h3 id="启动节点"><a href="#启动节点" class="headerlink" title="启动节点"></a>启动节点</h3><p><strong>一个节点就是一个运行在集群模式下的Redis服务器，Redis服务器在启动的时候根据<code>cluster-enabled</code>配置项的值来决定是否开启集群模式</strong>。如果是<code>yes</code>，开启集群模式成为一个节点；否则，开启单机模式成为一个普通服务器。</p>
<p>节点会继续使用<code>redisServer</code>结构保存服务器状态，使用<code>redisClient</code>结构保存客户端状态，至于那些只在集群模式下才会用到的数据，节点将它们保存在<code>cluster.h/clusterNode</code>结构，<code>cluster.h/clusterLink</code>结构和<code>cluster.h/clusterState</code>结构里面。</p>
<h3 id="集群数据结构"><a href="#集群数据结构" class="headerlink" title="集群数据结构"></a>集群数据结构</h3><p><code>clusterNode</code>结构保存了节点的当前状态，比如节点的创建时间、节点的名字、节点当前的配置纪元、节点的IP和端口等等。<strong>每个节点都会使用一个<code>clusterNode</code>结构记录自己的状态，并且会为集群中所有其他节点都创建一个相应的<code>clusterNode</code>结构</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">clusterNode</span> &#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建节点的时间</span></span><br><span class="line">    <span class="keyword">mstime_t</span> ctime;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 节点的名字，由40个十六进制字符组成</span></span><br><span class="line">    <span class="keyword">char</span> name[REDIS_CLUSTER_NAMELEN];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 节点标识</span></span><br><span class="line">    <span class="comment">// 使用不同的标识值记录节点的角色（比如主节点或者从节点）</span></span><br><span class="line">    <span class="comment">// 以及节点目前所处的状态（比如在线或者下线）</span></span><br><span class="line">    <span class="keyword">int</span> flags;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 节点当前的配置纪元，用于实现故障转移</span></span><br><span class="line">    <span class="keyword">uint64_t</span> configEpoch;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 节点的IP地址</span></span><br><span class="line">    <span class="keyword">char</span> ip[REDIS_IP_STR_LEN];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 节点的端口号</span></span><br><span class="line">    <span class="keyword">int</span> port;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 保存连接节点所需的有关信息</span></span><br><span class="line">    clusterLink *link;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>clusterNode结构的link属性是一个clusterLink结构，该结构保存了连接节点所需的有关信息，比如套接字描述符，输入缓存区和输出缓存区：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">clusterLink</span> &#123;</span></span><br><span class="line">    <span class="comment">// 连接的创建时间</span></span><br><span class="line">    <span class="keyword">mstime_t</span> ctime;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// TCP套接字描述符</span></span><br><span class="line">    <span class="keyword">int</span> fd;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 输出缓存区，保存着等待发送给其它节点的消息</span></span><br><span class="line">    sds sndbuf;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 输入缓存区，保存着从其他节点接收到的消息</span></span><br><span class="line">    sds rcvbuf;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 与这个连接相关联的节点，如果没有的话为NULL</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">clusterNode</span> *<span class="title">node</span>;</span></span><br><span class="line">&#125; clusterLink;</span><br></pre></td></tr></table></figure>

<p>最后，每个节点都保存着一个<code>RedisState</code>结构，记录了在当前节点视角下，集群目前所处的状态。例如集群是在线还是下线，集群中包含多少个节点，集群当前的配置纪元等。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">clusterState</span> &#123;</span></span><br><span class="line">    <span class="comment">// 指向当前节点的指针</span></span><br><span class="line">    clusterNode *myself;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集群当前的配置纪元，用于实现故障转移</span></span><br><span class="line">    <span class="keyword">unit64_t</span> currentEpoch;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集群当前的状态：是在线还是下线</span></span><br><span class="line">    <span class="keyword">int</span> state;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集群中至少处理着一个槽的节点数量</span></span><br><span class="line">    <span class="keyword">int</span> <span class="built_in">size</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集群节点名单</span></span><br><span class="line">    dict *nodes;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; clusterState;</span><br></pre></td></tr></table></figure>

<p>以前面介绍的7000、7001和7002为例，下图展示了节点7000创建的<code>clusterState</code>结构，这个结构从7000这个节点角度记录了集群以及集群包含的三个节点的当前状态。<br><img data-src="https://chentianming11.github.io/images/redis/cluster-state.png" alt="cluster-state"></p>
<ul>
<li><code>currentEpoch</code>的属性值为0，表示集群当前的配置纪元为0</li>
<li><code>size</code>属性值为0，表示集群目前没有任何节点在处理槽，因此<code>state</code>为<code>REDIS_CLUSTER_FAIL</code>，表示节点处于下线状态。</li>
<li><code>nodes</code>属性记录了集群目前包含的三个节点</li>
<li>三个节点的<code>flags</code>属性都是<code>REDIS_NODE_MASTER</code>，表示三个节点都是主节点。</li>
</ul>
<h2 id="槽指派"><a href="#槽指派" class="headerlink" title="槽指派"></a>槽指派</h2><p><strong>Redis集群通过分片的方式来保存数据库中的键值对：集群的数据库被分为16384个槽(slot)，数据库中的每个键都属于这16384个槽中的一个，集群中的每个节点可以处理0个或者最多16384个槽</strong>。</p>
<p><strong>当数据库中的16384个槽都有节点在处理时，集群处于上线状态(ok)，否则处理下线状态(fail)</strong>。</p>
<p>在上一节中，我们将7000、7001和7002三个节点连接到同一集群，不过此时，这个集群仍然处于下线状态，因为集群中的三个节点都没有在处理任何槽。通过向节点发送<code>CLUSTER ADDSLOTS</code>命令，可以将一个或者多个槽指派(assgin)给节点负责。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CLUSTER ADDSLOTS &lt;slot&gt; [slot ...]</span><br></pre></td></tr></table></figure>

<p>比如，我们可以将<code>0-5000</code>指派给7000负责，<code>5001-100000</code>指派给7002负责，<code>10001-16383</code>指派给7002负责。当完成指派之后，整个集群就会处于上线状态。</p>
<h3 id="记录节点的槽指派信息"><a href="#记录节点的槽指派信息" class="headerlink" title="记录节点的槽指派信息"></a>记录节点的槽指派信息</h3><p><code>clusterNode</code>结构的<code>slots</code>属性和<code>numslot</code>属性记录了节点负责处理的哪些槽：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">clusterNode</span> &#123;</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> slots[<span class="number">16384</span>/<span class="number">8</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> numslots;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>slots</code>属性是一个二进制数组，长度为<code>16384/8=2048</code>个字节，共<code>16384</code>位，刚好对应<code>16384</code>个槽。如果二进制位是1，那么该位置的槽就由当前节点处理。<code>numslot</code>属性记录了<code>slots</code>二进制数组中值为1的个数，也就是当前节点处理的槽的数量。</p>
<h3 id="传播节点的槽指派信息"><a href="#传播节点的槽指派信息" class="headerlink" title="传播节点的槽指派信息"></a>传播节点的槽指派信息</h3><p>一个节点除了会将自己负责处理的槽记录在<code>clusterNode</code>结构的<code>slots</code>属性和<code>numslot</code>属性中，还会将其发送给集群中的其它节点。这样的话，<strong>集群中的每个节点都会知道数据库中的16384个槽被指派给了哪些节点</strong>。</p>
<h3 id="记录集群所有的槽指派信息"><a href="#记录集群所有的槽指派信息" class="headerlink" title="记录集群所有的槽指派信息"></a>记录集群所有的槽指派信息</h3><p>通过前面的记录节点的槽指派信息和传播节点的槽指派信息，实际上集群中的每个节点都会知道数据库中的16384个槽被指派给了哪些节点。但是具体要知道某个槽对应的节点，上述的数据结构的时间复杂度是<code>O(N)</code>。为了提高效率，<code>clusterState</code>结构中的<code>slots</code>数组记录了所有16384个槽的指派信息。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">clusterState</span> &#123;</span></span><br><span class="line"></span><br><span class="line">    clusterNode *slots[<span class="number">16384</span>];</span><br><span class="line">&#125; clusterState;</span><br></pre></td></tr></table></figure>

<p><code>slots</code>数组包含16384个项，每个项是指向<code>clusterNode</code>的指针。这样的话，要知道某个槽对应的节点，时间复杂度就是<code>O(1)</code>。如果<code>slots[i]</code>指向<code>NULL</code>，就表示这个槽位没有被指派。</p>
<h2 id="在集群中执行命令"><a href="#在集群中执行命令" class="headerlink" title="在集群中执行命令"></a>在集群中执行命令</h2><p>在对数据16384个槽进行指派之后，集群就会进入上线状态。这时客户端节能向集群发送命令了。当客户端向节点发送与数据库键有关命令时，接收命令的节点会计算出命令要处理的数据库键属于哪个槽，然后检查这个槽是否指派给了自己：</p>
<ul>
<li>如果指派给了自己，那么当前节点直接执行这个命令。</li>
<li>如果指派给了其它节点，那么当前节点会向客户端返回<code>MOVED</code>错误(包含这个键所属的节点)，客户端再转向正确的节点发送数据命令。</li>
</ul>
<p><img data-src="https://chentianming11.github.io/images/redis/cluster-command.png" alt="cluster-command"></p>
<h3 id="计算键属于哪个槽"><a href="#计算键属于哪个槽" class="headerlink" title="计算键属于哪个槽"></a>计算键属于哪个槽</h3><p>节点使用一下算法来计算给定键key属于哪个槽：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">def <span class="title">slot_number</span><span class="params">(key)</span>:</span></span><br><span class="line"><span class="function">    <span class="keyword">return</span> <span class="title">CRC16</span><span class="params">(key)</span> &amp; 16383</span></span><br></pre></td></tr></table></figure>

<p>使用<code>CLUSTER KEYSLOT &lt;key&gt;</code>命令可以查看给定键属于哪个槽。</p>
<h3 id="命令处理"><a href="#命令处理" class="headerlink" title="命令处理"></a>命令处理</h3><p>当计算出键所属的槽i之后，接下来就会到<code>clusterState.slots[i]</code>找到这个槽对应的节点。如果该节点就是当前节点，那么直接执行命令，否则，返回<code>MOVED</code>错误，<code>MOVED</code>错误格式为<code>MOVED &lt;slot&gt; &lt;ip&gt;:&lt;port&gt;</code>。这样，客户端在收到<code>MOVED</code>错误之后，就能将命令转向给正确的节点执行了。</p>
<h3 id="节点数据库实现"><a href="#节点数据库实现" class="headerlink" title="节点数据库实现"></a>节点数据库实现</h3><p>节点和单机服务器在数据库方面有一个区别，<strong>节点只能使用0号数据库</strong>。另外，除了将键值对保存在数据库之外，节点还会用<code>clusterState</code>结构中的<code>slots_to_keys</code>跳跃表来保存槽和键的关系。跳跃表的<code>score</code>是槽号，<code>value</code>是数据库键。<strong>通过<code>slots_to_keys</code>跳跃表，节点可以很方便的对某个或者某些槽的数据库键进行批量操作</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">clusterState</span> &#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    zskiplist *slots_to_keys;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125; clusterState;</span><br></pre></td></tr></table></figure>

<h2 id="重新分片"><a href="#重新分片" class="headerlink" title="重新分片"></a>重新分片</h2><p><strong><code>Redis</code>集群的重新分片操作可以将任意数量的已经指派给某个节点（源节点）的槽改为指派给另一个节点（目标节点），并且相关槽所属的键值对也会从源节点移动到目标节点</strong>。</p>
<p>重新分片操作可以在线（online）进行，在重新分片的过程中，集群不需要下线，并且源节点和目标节点都可以继续处理请求。</p>
<p>比如，我们新启动一个端口为7003的服务器，将原本指派给7002的<code>15001-16383</code>的槽改指派给节点7003。</p>
<h3 id="重新分片实现原理"><a href="#重新分片实现原理" class="headerlink" title="重新分片实现原理"></a>重新分片实现原理</h3><p>Redis集群的重新分片操作是由Redis的集群管理软件<code>redis-trib</code>负责执行的。<code>redis-trib</code>对集群单个槽slot重新分片步骤如下：</p>
<ol>
<li><code>redis-trib</code>对目标节点发送<code>CLUSTER SETSLOT &lt;slot&gt; IMPORTING &lt;source_id&gt;</code>命令，让目标节点准备好从源节点导入槽slot的键值对。</li>
<li><code>redis-trib</code>对源节点发送<code>CLUSTER SETSLOT &lt;slot&gt; MIGRATING &lt;target_id&gt;</code>命令，让源节点准备好将属于槽slot的键值对迁移(migrate)至目标节点。</li>
<li><code>redis-trib</code>向源节点发送<code>CLUSTER GETKEYSINSLOT &lt;slot&gt; &lt;count&gt;</code>命令，获取最多count个属于槽slot的键名。</li>
<li>对于步骤三的每个键名，<code>redis-trib</code>都向源节点发送一个<code>MIGRATE &lt;target_id&gt; &lt;target_port&gt; &lt;key_name&gt; 0 &lt;time_out&gt;</code>命令，将该键原子地从源节点迁移到目标节点。</li>
<li>重新执行步骤三和四，直到槽slot的键值对全部迁移至目标节点。</li>
<li><code>redis-trib</code>向集群任意一个节点发送<code>CLUSTER SETSLOT &lt;slot&gt; NODE &lt;target_id&gt;</code>命令，将槽slot指派给目标节点。并且指派信息会通过消息发送到整个集群。<br><img data-src="https://chentianming11.github.io/images/redis/cluster-migrate.png" alt="cluster-migrate"></li>
</ol>
<p>如果重新分片涉及多个槽，就对每个槽执行上述步骤。对slot槽进行重新分片过程如下：<br><img data-src="https://chentianming11.github.io/images/redis/cluster-sharding.png" alt="cluster-sharding"></p>
<h3 id="ASK错误"><a href="#ASK错误" class="headerlink" title="ASK错误"></a>ASK错误</h3><p>在重新分片期间，源节点向目标节点迁移一个槽的过程中，可能出现属于被迁移槽的一部分键值对保存在源节点里面，而另一部分键值对则保存在目标节点里面。当客户端向源节点发送数据库键相关的命令，并且数据库键恰好就属于正在被迁移的槽时。<strong>源节点首先会在自己的数据库里面查找该数据库键，如果找到则直接在源节点执行命令，否则，源节点返回一个<code>ASK</code>错误，指引客户端转向目标节点</strong>。<br><img data-src="https://chentianming11.github.io/images/redis/cluster-ask.png" alt="cluster-ask"></p>
<h2 id="复制与故障转移"><a href="#复制与故障转移" class="headerlink" title="复制与故障转移"></a>复制与故障转移</h2><p><strong><code>Redis</code>集群中的节点分为主节点和从节点，主节点用于处理槽，从节点用于复制某个主节点，并且在主节点下线时代替主节点继续进行客户端命令</strong>。举个栗子，假设有7000、7001、7002、7003四个主节点，其中还有7004和7005作为70000的从节点。当7000节点下线时，会从7004和7005中选举出一个（假设选中了7004）作为代替7000的主节点。当7000重新上线时，7000会作为7004的从节点。<br><img data-src="https://chentianming11.github.io/images/redis/cluster-failover.png" alt="cluster-failover"></p>
<blockquote>
<p>主节点用双圆环表示。</p>
</blockquote>
<h3 id="设置从节点"><a href="#设置从节点" class="headerlink" title="设置从节点"></a>设置从节点</h3><p>向节点发送<code>CLUSTER REPLICATE &lt;node_id&gt;</code>命令，可以让该节点成为<code>node_id</code>的从节点。</p>
<h3 id="故障检测"><a href="#故障检测" class="headerlink" title="故障检测"></a>故障检测</h3><p>集群中每个节点都会定期向其他节点发送<code>PING</code>消息，以此来检测对方是否在线。如果对方节点在规定时间内没有返回<code>PONG</code>消息，那么发送方节点就会将对应的节点标记为疑似下线状态(PFIAL)。<strong>如果一个集群内，半数以上主节点都将某个主节点x标记为疑似下线状态，那么这个节点x就会被标记为已下线状态(FAIL)</strong>。</p>
<blockquote>
<p>关键词：心跳检测+过半原则</p>
</blockquote>
<h3 id="故障转移"><a href="#故障转移" class="headerlink" title="故障转移"></a>故障转移</h3><p>当从节点发现自己复制的主节点进入已下线状态时，从节点就开始对下线主节点进行故障转移，以下是故障转移的执行步骤。</p>
<ol>
<li>基于<code>Raft</code>算法，选举出一个从节点。</li>
<li>被选中的从节点执行<code>SLAVEOF no one</code>，成为主节点。</li>
<li>新的主节点会撤销已下线主节点的槽指派，并将这些槽全部指派给自己。</li>
<li>新的主节点向集群广播一条<code>PONG</code>消息，让集群中其它节点知道新的主节点已经代替了已下线主节点。</li>
<li>新的主节点开始接收和处理自己负责的槽有关的命令请求，故障转移完成。</li>
</ol>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><h3 id="优势"><a href="#优势" class="headerlink" title="优势"></a>优势</h3><ol>
<li>无中心架构。</li>
<li>数据按照 slot 存储分布在多个节点，节点间数据共享，可动态调整数据分布。</li>
<li>可扩展性，可线性扩展到 1000 个节点(官方推荐不超过 1000 个)，节点可动态添加或删除。</li>
<li>高可用性，部分节点不可用时，集群仍可用。通过增加 Slave 做 standby 数据副本，能够实现故障自动<code>failover</code>，节点之间通过 <code>gossip</code>协议交换状态信息，用<code>Raft</code>投票机制完成<code>Slave</code>到<code>Master</code>的角色提升。</li>
<li>降低运维成本，提高系统的扩展性和可用性。</li>
</ol>
<h3 id="不足"><a href="#不足" class="headerlink" title="不足"></a>不足</h3><ol>
<li><code>Client</code>实现复杂，驱动要求实现<code>Smart Client</code>，缓存<code>slots mapping</code>信息并及时更新，提高了开发难度，客户端的不成熟影响业务的稳定性。</li>
<li><strong>节点会因为某些原因发生阻塞(阻塞时间大于 clutser-node-timeout)，被判断下线，这种 failover 是没有必要的。</strong></li>
<li>数据通过异步复制，不保证数据的强一致性。</li>
</ol>
<blockquote>
<p>原创不易，觉得文章写得不错的小伙伴，点个赞👍 鼓励一下吧~</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/redis/" rel="tag"># redis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/3441087838/" rel="prev" title="Redis分布式-主从复制详解">
      <i class="fa fa-chevron-left"></i> Redis分布式-主从复制详解
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/3833574358/" rel="next" title="推荐一个适用于SpringBoot项目的轻量级HTTP客户端框架，快来试试它!">
      推荐一个适用于SpringBoot项目的轻量级HTTP客户端框架，快来试试它! <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#节点"><span class="nav-number">1.</span> <span class="nav-text">节点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动节点"><span class="nav-number">1.1.</span> <span class="nav-text">启动节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#集群数据结构"><span class="nav-number">1.2.</span> <span class="nav-text">集群数据结构</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#槽指派"><span class="nav-number">2.</span> <span class="nav-text">槽指派</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#记录节点的槽指派信息"><span class="nav-number">2.1.</span> <span class="nav-text">记录节点的槽指派信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#传播节点的槽指派信息"><span class="nav-number">2.2.</span> <span class="nav-text">传播节点的槽指派信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录集群所有的槽指派信息"><span class="nav-number">2.3.</span> <span class="nav-text">记录集群所有的槽指派信息</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#在集群中执行命令"><span class="nav-number">3.</span> <span class="nav-text">在集群中执行命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#计算键属于哪个槽"><span class="nav-number">3.1.</span> <span class="nav-text">计算键属于哪个槽</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#命令处理"><span class="nav-number">3.2.</span> <span class="nav-text">命令处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#节点数据库实现"><span class="nav-number">3.3.</span> <span class="nav-text">节点数据库实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#重新分片"><span class="nav-number">4.</span> <span class="nav-text">重新分片</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#重新分片实现原理"><span class="nav-number">4.1.</span> <span class="nav-text">重新分片实现原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ASK错误"><span class="nav-number">4.2.</span> <span class="nav-text">ASK错误</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复制与故障转移"><span class="nav-number">5.</span> <span class="nav-text">复制与故障转移</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#设置从节点"><span class="nav-number">5.1.</span> <span class="nav-text">设置从节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#故障检测"><span class="nav-number">5.2.</span> <span class="nav-text">故障检测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#故障转移"><span class="nav-number">5.3.</span> <span class="nav-text">故障转移</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#优势"><span class="nav-number">6.1.</span> <span class="nav-text">优势</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#不足"><span class="nav-number">6.2.</span> <span class="nav-text">不足</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="陈添明"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">陈添明</p>
  <div class="site-description" itemprop="description">一个爱看国漫的程序猿</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">陈添明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">156k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:30</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-ribbon@1/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-pjax@0/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  
  <script src="//cdn.jsdelivr.net/npm/quicklink@1/dist/quicklink.umd.js"></script>
  <script>
      window.addEventListener('load', () => {
      quicklink({
        timeout : 3000,
        priority: true,
        ignores : [uri => uri.includes('#'),uri => uri === 'https://chentianming11.github.io/posts/2663121991/',]
      });
      });
  </script>


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'SoPxUkme2aynTULaNuIc5TIq-gzGzoHsz',
      appKey     : '4TDP7SMCbgDKTOGGUtpIeA0H',
      placeholder: "留下点足迹吧~",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

    </div>
</body>
</html>
